using UnityEngine;
using UnityEngine.UI;
using QFramework;
using DG.Tweening;

namespace HareGame
{
    public class UISettingData : UIPanelData
    {
    }
    public partial class UISetting : UIBase
    {

        public int GMClickTimes;
        protected override void OnInit(IUIData uiData = null)
        {
            base.OnInit();
            mData = uiData as UISettingData ?? new UISettingData();
        }

        protected override void OnOpen(IUIData uiData = null)
        {
            base.OnOpen();

            GMClickTimes = 0;
            GameEntry.Util.DoScaleWithMask(Mask, Bg);
            btnMusicOn.gameObject.SetActive(GameEntry.Audio.BGMVolume == 1);
            btnMusicOff.gameObject.SetActive(GameEntry.Audio.BGMVolume == 0);
            btnSoundOn.gameObject.SetActive(GameEntry.Audio.SoundVolume == 1);
            btnSoundOff.gameObject.SetActive(GameEntry.Audio.SoundVolume == 0);
            btnVibrationOn.gameObject.SetActive(GameEntry.Util.IsEnableVibrate);
            btnVibrationOff.gameObject.SetActive(!GameEntry.Util.IsEnableVibrate);

            btnMusicOn.onClick.AddListener(OnMusicClick);
            btnMusicOff.onClick.AddListener(OnMusicClick);
            btnSoundOn.onClick.AddListener(OnSoundClick);
            btnSoundOff.onClick.AddListener(OnSoundClick);
            btnVibrationOn.onClick.AddListener(OnVibration);
            btnVibrationOff.onClick.AddListener(OnVibration);
            btnClose.onClick.AddListener(OnCloseClick);
        }

        protected override void OnClose()
        {
            base.OnClose();

            btnMusicOn.onClick.RemoveListener(OnMusicClick);
            btnMusicOff.onClick.RemoveListener(OnMusicClick);
            btnSoundOn.onClick.RemoveListener(OnSoundClick);
            btnSoundOff.onClick.RemoveListener(OnSoundClick);
            btnVibrationOn.onClick.RemoveListener(OnVibration);
            btnVibrationOff.onClick.RemoveListener(OnVibration);
        }

        void OnMusicClick()
        {
            GMClickTimes++;
            if (GMClickTimes >= 15)
            {
                UIGM.Instance.ShowGMPanel();
                UIGM.Instance.btnGM.gameObject.SetActive(true);
            }
            GameEntry.Audio.BGMVolume = GameEntry.Audio.BGMVolume == 0 ? 1 : 0;
            btnMusicOn.gameObject.SetActive(GameEntry.Audio.BGMVolume == 1);
            btnMusicOff.gameObject.SetActive(GameEntry.Audio.BGMVolume == 0);
        }

        void OnSoundClick()
        {
            GameEntry.Audio.SoundVolume = GameEntry.Audio.SoundVolume == 0 ? 1 : 0;
            btnSoundOn.gameObject.SetActive(GameEntry.Audio.SoundVolume == 1);
            btnSoundOff.gameObject.SetActive(GameEntry.Audio.SoundVolume == 0);
        }

        void OnVibration()
        {
            GameEntry.Util.IsEnableVibrate = !GameEntry.Util.IsEnableVibrate;
            btnVibrationOn.gameObject.SetActive(GameEntry.Util.IsEnableVibrate);
            btnVibrationOff.gameObject.SetActive(!GameEntry.Util.IsEnableVibrate);
        }

        void OnCloseClick()
        {
            GameEntry.Util.DoHideWithMask(Mask, Bg).OnComplete(() =>
             {
                 GameEntry.UI.CloseUI(this);
             });
        }
    }
}
